package com.start.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.admin.entity.StartSysDept;
import com.start.admin.mappers.SysDeptMapper;
import com.start.admin.service.StartSysDeptService;
import com.start.common.exceptions.StartBusException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class StartSysDeptServiceImpl extends ServiceImpl<SysDeptMapper, StartSysDept> implements StartSysDeptService {
    @Override
    public void createEntity(StartSysDept entity) {
        if (StringUtils.isEmpty(entity.getParentId())) {
            save(entity);
            return;
        }
        StartSysDept parentDept = getById(entity.getParentId());
        if (parentDept == null) throw new StartBusException("父级机构不存在！");
        String levelIds = parentDept.getLevelParentIds();
        if (StringUtils.isEmpty(levelIds)) {
            levelIds = "";
        }
        levelIds += parentDept.getId() + ",";
        entity.setLevelParentIds(levelIds);
        save(entity);
    }
}
